package com.ulook.lookplanet.service;

import com.ulook.lookplanet.bean.*;
import com.ulook.lookplanet.common.ComPath;
import com.ulook.lookplanet.mapper.PubMapper;
import com.ulook.lookplanet.util.FileUtil;
import com.ulook.lookplanet.util.ThrowException;
import com.ulook.lookplanet.util.mgc.SensitiveUtil;
import com.ulook.lookplanet.util.mgc.SensitiveWordInit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 描述：
 *
 * @author L1998
 * @date 2021/4/10
 */
@Service
public class PubService {

    @Autowired
    private PubMapper mapper;

    @Autowired
    private SensitiveWordInit sensitiveWordInit;

    /**
     * 获取话题分组
     */
    public List<TCommunity> getComList(){
        return mapper.getComList();
    }

    /**
     * 获取话题
     */
    public List<TTopic> getTopicByComId(String comId){
        return mapper.getTopicByComId(comId);
    }

    /**
     * 发布
     */
    @Transactional
    public void pubArticle(TArticle article, List<MultipartFile> files){

        // 屏蔽敏感词
        try {
            //将文本中的敏感词读取出来存入List集合
            ClassPathResource classPathResource = new ClassPathResource("/static/maren.txt");
            List<String> list = FileUtil.readFile2List(classPathResource.getFile());
            //初始化词库
            Map sensitiveWordMap = sensitiveWordInit.initSensitiveWord(list);
            //初始化传入SensitiveUtil的敏感词库
            SensitiveUtil.sensitiveWordMap = sensitiveWordMap;
            String content = article.getContent();
            String re = SensitiveUtil.replaceSensitiveWord(content, 2, "*");
            article.setContent(re);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 保存到数据库
        mapper.addArticle(article);

        String audioName = article.getAudrofile();
        // 是否有音频
        if(audioName != null){
            MultipartFile audio = files.remove(0);
            if(!audio.isEmpty()){
                try {
                    // 项目所在目录
                    String jar_parent  = new File(ResourceUtils.getURL("classpath:").getPath()).getParentFile().getParentFile().getParent();
                    jar_parent+= File.separator+ ComPath.FILE_DIR+File.separator;
                    String path = ComPath.FILE_DIR_SERVICE + File.separator + ComPath.AUDIO_PATH + File.separator;
                    // 保存在本地
                    File audio2 = new File(jar_parent + audioName);
                    audio.transferTo(audio2);
                } catch (FileNotFoundException e) {
                    ThrowException.print(e, "获取项目所在目录失败");
                } catch (IOException e) {
                    ThrowException.print(e, "音频保存失败");
                }
            }
        }

        // 保存图片
        ArrayList<TArtPic> artPics = new ArrayList<>();
        for(int i = 0; i < files.size(); i++){
            MultipartFile image = files.get(i);
            System.out.println(image.getOriginalFilename()+"   ######   "+image.getName());
            try {
                // 项目所在目录
                String jar_parent  = new File(ResourceUtils.getURL("classpath:").getPath()).getParentFile().getParentFile().getParent();
                jar_parent+= File.separator+ComPath.FILE_DIR+File.separator+ComPath.ARTICLE_IMG_PATH+File.separator;
                String path = ComPath.FILE_DIR_SERVICE + File.separator + ComPath.ARTICLE_IMG_PATH + File.separator;
                // 保存在本地
                File image2 = new File(jar_parent + image.getOriginalFilename());
                image.transferTo(image2);
                TArtPic artPic = new TArtPic();
                artPic.setUid(image.getOriginalFilename());
                artPic.setArtid(article.getUid());
                artPic.setSort(i);
                artPics.add(artPic);
            } catch (FileNotFoundException e) {
                ThrowException.print(e, "获取项目所在目录失败");
            } catch (IOException e) {
                ThrowException.print(e, "音频保存失败");
            }
        }
        if(artPics.size() > 0){
            mapper.addArtPic(artPics);
        }

    }


    /**
     * 保存选中的话题
     */
     public void addArtTopic(TArtTopic artTopic){
        mapper.addArtTopic(artTopic);
    }


}
